package 
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.display.StageScaleMode;
	import flash.geom.Rectangle;
	import com.Avatar;
	import com.Map;
	import com.Stats;
	import com.GridLayer;
	
	/**
	 * ...
	 * @author anzhilin
	 */
	public class Main extends Sprite 
	{
		private const XML_PATH:String = "./map/map_grids.xml";
		private var map:Map;
		private var avatar:Avatar;
		private var arrowLeft:Boolean = false;
		private var arrowRight:Boolean = false;
		private var arrowUp:Boolean = false;
		private var arrowDown:Boolean = false;
		private var rect:Rectangle;
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			//var state:Stats = new Stats();
			//stage.addChild(state);
			//stage.setChildIndex(state,stage.numChildren-1);
			stage.scaleMode = StageScaleMode.NO_SCALE;
			var loader:URLLoader = new URLLoader(new URLRequest(XML_PATH));
			rect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			loader.addEventListener(Event.COMPLETE, initMap);
			
			var playerLoader:Loader = new Loader();
			playerLoader.load(new URLRequest('./images/woman.png'));
			playerLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,initPlayer);
		}
		private function initMap(evt:Event):void
		{
			
			var mapXml:XML = new XML(evt.target.data);
			map = new Map(mapXml);
			addChild(map);
			map.addEventListener(MouseEvent.CLICK, clickMap);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			stage.addEventListener(Event.ENTER_FRAME,gameLoop);
		}
		private function initPlayer(evt:Event):void
		{
			var bmd:BitmapData = evt.target.content.bitmapData;
			avatar = new Avatar(bmd);
			map.addPlayer(avatar);
			map.controlPlayer(rect.x,rect.y);
		}
		private function gameLoop(evt:Event):void
		{
			stage.frameRate *= 1;
			map.scrollRect = rect;
			///trace(avatar.x);
			if (arrowLeft)	rect.x -= 4;
			if (arrowRight)	rect.x += 4;
			if (arrowDown)	rect.y += 4;
			if (arrowUp)	rect.y -= 4;
			if (rect.x < 0)
			{
				rect.x = 0;
			}
			else if (rect.x > map.mapWidth - rect.width)
			{
				rect.x = map.mapWidth-rect.width;
			}
			if (rect.y < 0)
			{
				rect.y = 0;
			}
			else if (rect.y > map.mapHeight - rect.height)
			{
				rect.y = map.mapHeight - rect.height;
			}
		}
		private function onKeyDown(evt:KeyboardEvent):void
		{
			if (evt.keyCode == 37)
			{
				arrowLeft = true;
			}
			else if (evt.keyCode == 38)
			{
				arrowUp = true;
			}
			else if (evt.keyCode == 39)
			{
				arrowRight = true;
			}
			else if (evt.keyCode == 40)
			{
				arrowDown = true;
			}
		}
		private function onKeyUp(evt:KeyboardEvent):void
		{
			if (evt.keyCode == 37)
			{
				arrowLeft = false;
			}
			else if (evt.keyCode == 38)
			{
				arrowUp = false;
			}
			else if (evt.keyCode == 39)
			{
				arrowRight = false;
			}
			else if (evt.keyCode == 40)
			{
				arrowDown = false;
			}
		}
		private var a:int = 0;
		private function clickMap(evt:MouseEvent):void
		{			
			var mX:int = map.mouseX;
			var mY:int = map.mouseY;
			var worldPoint:Point = Map.screenToworld(mX, mY);
			//avatar.moveTo(new Point(0,0),new Point(0,0));
			trace(worldPoint);
			for (var i:int = 0; i < map._buildingLayer.buildingArr.length; i++ )
			{
				
				if (avatar.hitTestObject(map._buildingLayer.buildingArr[i]))
				{
					trace("hit");
					if (avatar.y < map._buildingLayer.buildingArr[i].y)
					{
						trace(a++);
						map._buildingLayer.swapChildren(avatar, map._buildingLayer.buildingArr[i]);
					}
				}
			}
			//trace(worldPoint);
		}
	}
	
}